package com.carlinfo.stu_ques.common.util;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtile implements Serializable
{
	/* 默认的模式字符串 */
	private String dateTimePattern = "yyyy-MM-dd HH:mm:ss"; 
	private String datePattern = "yyyy-MM-dd";
	
	/**
	 * 将Date变成有格式的字符串
	 * @param date
	 * @return
	 */
	public String dateToStr(Date date)
	{
		/* 模式字符串 */
		SimpleDateFormat sdf = new SimpleDateFormat(this.datePattern);
		/* 将Date变成字符串 */
		return sdf.format(date);
	}
	
	/**
	 * 将Date变成有格式的字符串
	 * @param date
	 * @return
	 */
	public String dateToStr(Date date,String pattern)
	{
		/* 模式字符串 */
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		/* 将Date变成字符串 */
		return sdf.format(date);
	}
	
	/**
	 * 将Date变成有格式的字符串
	 * @param date
	 * @return
	 */
	public String dateTimeToStr(Date date)
	{
		/* 模式字符串 */
		SimpleDateFormat sdf = new SimpleDateFormat(this.dateTimePattern);
		/* 将Date变成字符串 */
		return sdf.format(date);
	}
	
	/**
	 * 将字符串变成有格式的Date
	 * @param date
	 * @return
	 */
	public Date strToDate(String dateStr)
	{
		/* 模式字符串 */
		SimpleDateFormat sdf = new SimpleDateFormat(this.datePattern);
		try
		{
			/* 将Date变成字符串 */
			return sdf.parse(dateStr);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("==时间格式有问题,会变成当前时间==");
		}
		/* 如果转换失败,为了不报空指针,返回当前时间 */
		return new Date() ; 
	}
	
	/**
	 * 将Date变成有格式的字符串
	 * @param date
	 * @return
	 */
	public Date strToDateTime(String dateStr,String pattern)
	{
		/* 模式字符串 */
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try
		{
			/* 将Date变成字符串 */
			return sdf.parse(dateStr);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("==时间格式有问题,会变成当前时间==");
		}
		/* 如果转换失败,为了不报空指针,返回当前时间 */
		return new Date() ; 
	}
	
	/**
	 * 将Date变成有格式的字符串
	 * @param date
	 * @return
	 */
	public Date strToDateTime(String dateStr)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(this.dateTimePattern);
		try
		{
			/* 将Date变成字符串 */
			return sdf.parse(dateStr);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		/* 如果转换失败,为了不报空指针,返回当前时间 */
		return new Date() ; 
	}
	
	public boolean isyesDay(Date nowDate,Date oldDate)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowDate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date enDate = calendar.getTime();
		calendar.set(Calendar.DATE, -1);
		Date stDate = calendar.getTime();
		
		return oldDate.after(stDate) && oldDate.before(enDate);
	}
}
